<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggle" :class="{ active: active }">
      <span class="placeholder" v-if="!fullLocation">请选择配送地址</span>
      <span class="value" v-else>
        {{ fullLocation }}
      </span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="active">
      <span @click="selectCity(item)" class="ellipsis" v-for="item in cityList" :key="item.code">{{item.name}}</span>
    </div>
  </div>
</template>

<script>
import { ref, reactive, watch } from 'vue'
import { onClickOutside } from '@vueuse/core'
import { getCityData } from '@/api/product'
export default {
  name: 'XtxCity',
  props: {
    fullLocation: {
      type: String
    }
  },
  setup (props, { emit }) {
    const active = ref(false)
    const target = ref(null)
    const open = () => {
      active.value = true
    }
    const close = () => {
      active.value = false
    }

    const toggle = () => {
      active.value ? close() : open()
    }

    // 点击盒子外部会触发事件
    onClickOutside(target, () => {
      close()
    })

    // 获取省市区
    const cityList = ref([]) //  用于渲染
    const cacheList = ref([]) //  用于回显
    getCityData().then(res => {
      cityList.value = res
      cacheList.value = res
    })

    // 选择城市
    const changeResult = reactive({
      provinceCode: '',
      provinceName: '',
      cityCode: '',
      cityName: '',
      countyCode: '',
      countyName: ''
    })
    const selectCity = city => {
      if (city.level === 0) {
        // 省
        changeResult.provinceCode = city.code
        changeResult.provinceName = city.name
        cityList.value = city.areaList
      } else if (city.level === 1) {
        // 市
        changeResult.cityCode = city.code
        changeResult.cityName = city.name
        cityList.value = city.areaList
      } else if (city.level === 2) {
        // 区
        changeResult.countyCode = city.code
        changeResult.countyName = city.name
        close()
        // 选择后的数据传给父组件
        emit('changeCity', changeResult)
      }
    }

    // 关闭时恢复数据
    watch(active, (value) => {
      if (!value) {
        // 重置数据
        cityList.value = cacheList.value
      }
    })

    return {
      active,
      open,
      close,
      toggle,
      target,
      cityList,
      selectCity,
      changeResult
    }
  }
}
</script>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
